package com.lvmama.dashboard.service.api.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lvmama.dashboard.dao.api.IApiSummaryDao;
import com.lvmama.dashboard.entity.ApiSummary;
import com.lvmama.dashboard.service.api.IApiSummaryService;

@Service
public class ApiSummaryServiceImpl implements IApiSummaryService {
	
	@Autowired
	private IApiSummaryDao apiSummaryDao;
	
	@Override
	@Transactional
	public int saveApiSummary(List<ApiSummary> list) {
		if(CollectionUtils.isEmpty(list)) {
			return 0;
		}
		int total = 0;
		for(ApiSummary apiSummary : list) {
			ApiSummary temp = apiSummaryDao.queryByApi(apiSummary.getApi());
			if(temp != null) {
				continue;
			}
			int insertResult = apiSummaryDao.insert(apiSummary);
			total = total + insertResult;
		}
		return total;
	}

	@Override
	public List<ApiSummary> queryApiSummaryList(int page, int pageSize) {
		if (page <= 0) {
			page = 1;
		}
		if (pageSize < 0) {
			pageSize = 20;
		}
		return apiSummaryDao.queryAll((page - 1) * pageSize, page * pageSize);
	}

}
